namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class MenuNodeWriter : IMenuNodeWriter
    {
        private MenuNode m_objNode;

        private string GetNodeCss(MenuNode oNode)
        {
            string cssClass = oNode.DNNMenu.CssClass;
            if (oNode.Level > 0)
            {
                cssClass = oNode.DNNMenu.DefaultChildNodeCssClass;
            }
            if (Strings.Len(oNode.CSSClass) > 0)
            {
                cssClass = oNode.CSSClass;
            }
            if (!oNode.Selected)
            {
                return cssClass;
            }
            if (Strings.Len(oNode.CSSClassSelected) > 0)
            {
                return (cssClass + " " + oNode.CSSClassSelected);
            }
            return (cssClass + " " + oNode.DNNMenu.DefaultNodeCssClassSelected);
        }

        protected void Render(HtmlTextWriter writer)
        {
            MenuNode node=null;
            string iD=String.Empty;
            if (this.m_objNode.DNNMenu.SelectedMenuNodes.Count > 0)
            {
                node = (MenuNode) this.m_objNode.DNNMenu.SelectedMenuNodes[this.m_objNode.DNNMenu.SelectedMenuNodes.Count];
            }
            if (this.m_objNode.Parent != null)
            {
                iD = this.m_objNode.Parent.ID;
            }
            if ((((node == null) || this.m_objNode.Selected) || (StringType.StrCmp(node.ID, this.m_objNode.ID, false) == 0)) || (StringType.StrCmp(node.ID, iD, false) == 0))
            {
                this.RenderContents(writer);
            }
            if (this.m_objNode.HasNodes && this.m_objNode.Selected)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "Child");
                writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                this.RenderChildren(writer);
                writer.RenderEndTag();
            }
        }

        protected void RenderChildren(HtmlTextWriter writer)
        {
            IEnumerator enumerator=null;
            try
            {
                enumerator = this.m_objNode.MenuNodes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ((MenuNode) enumerator.Current).Render(writer);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    ((IDisposable) enumerator).Dispose();
                }
            }
        }

        protected void RenderContents(HtmlTextWriter writer)
        {
            this.RenderOpenTag(writer);
            this.RenderNodeIcon(writer);
            this.RenderNodeText(writer);
            this.RenderNodeArrow(writer);
            writer.RenderEndTag();
        }

        public void RenderNode(HtmlTextWriter writer, MenuNode Node)
        {
            this.m_objNode = Node;
            this.Render(writer);
        }

        protected void RenderNodeArrow(HtmlTextWriter writer)
        {
            if (this.m_objNode.HasNodes)
            {
                Label label = new Label();
                label.RenderBeginTag(writer);
                if (this.m_objNode.Level == 0)
                {
                    if (Strings.Len(this.m_objNode.DNNMenu.RootArrowImage) > 0)
                    {
                        Image image = new Image();
                        image.ImageUrl = this.m_objNode.DNNMenu.RootArrowImage;
                        image.RenderControl(writer);
                    }
                }
                else if (Strings.Len(this.m_objNode.DNNMenu.ChildArrowImage) > 0)
                {
                    Image image2 = new Image();
                    image2.ImageUrl = this.m_objNode.DNNMenu.ChildArrowImage;
                    image2.RenderControl(writer);
                }
                label.RenderEndTag(writer);
            }
        }

        protected void RenderNodeIcon(HtmlTextWriter writer)
        {
            Label label = new Label();
            if (Strings.Len(this.m_objNode.CSSIcon) > 0)
            {
                label.CssClass = this.m_objNode.CSSIcon;
            }
            else if (Strings.Len(this.m_objNode.DNNMenu.DefaultIconCssClass) > 0)
            {
                label.CssClass = this.m_objNode.DNNMenu.DefaultIconCssClass;
            }
            label.RenderBeginTag(writer);
            if (this.m_objNode.ImageIndex > -1)
            {
                NodeImage image = this.m_objNode.DNNMenu.ImageList[this.m_objNode.ImageIndex];
                if (image != null)
                {
                    Image image2 = new Image();
                    image2.ImageUrl = image.ImageUrl;
                    image2.RenderControl(writer);
                    writer.Write("&nbsp;");
                }
            }
            label.RenderEndTag(writer);
        }

        protected void RenderNodeText(HtmlTextWriter writer)
        {
            HyperLink link = new HyperLink();
            string expression = "";
            link.Text = this.m_objNode.Text;
            if (Strings.Len(this.m_objNode.JSFunction) > 0)
            {
                if (!this.m_objNode.JSFunction.EndsWith(";"))
                {
                    MenuNode objNode = this.m_objNode;
                    objNode.JSFunction = objNode.JSFunction + ";";
                }
                expression = expression + this.m_objNode.JSFunction;
            }
            else if (Strings.Len(this.m_objNode.DNNMenu.JSFunction) > 0)
            {
                if (!this.m_objNode.DNNMenu.JSFunction.EndsWith(";"))
                {
                    DNNMenu dNNMenu = this.m_objNode.DNNMenu;
                    dNNMenu.JSFunction = dNNMenu.JSFunction + ";";
                }
                expression = expression + this.m_objNode.DNNMenu.JSFunction;
            }
            if (this.m_objNode.Enabled)
            {
                switch (this.m_objNode.ClickAction)
                {
                    case eClickAction.PostBack:
                    case eClickAction.Expand:
                    case eClickAction.None:
                        if (Strings.Len(expression) > 0)
                        {
                            expression = "if (eval(\"" + expression.Replace("\"", "\"\"") + "\") != false) ";
                        }
                        expression = expression + this.m_objNode.DNNMenu.Page.GetPostBackEventReference(this.m_objNode.DNNMenu, this.m_objNode.ID + ClientAPI.COLUMN_DELIMITER + "Click");
                        break;

                    case eClickAction.Navigate:
                        if (Strings.Len(expression) > 0)
                        {
                            expression = "if (eval(\"" + expression.Replace("\"", "\"\"") + "\") != false) ";
                        }
                        if (Strings.Len(this.m_objNode.DNNMenu.Target) > 0)
                        {
                            expression = expression + "window.frames." + this.m_objNode.DNNMenu.Target + ".location.href='" + this.m_objNode.NavigateURL + "'; void(0);";
                        }
                        else
                        {
                            expression = expression + "window.location.href='" + this.m_objNode.NavigateURL + "';";
                        }
                        break;
                }
                link.NavigateUrl = "javascript:" + expression;
            }
            if (Strings.Len(this.m_objNode.ToolTip) > 0)
            {
                link.ToolTip = this.m_objNode.ToolTip;
            }
            string nodeCss = this.GetNodeCss(this.m_objNode);
            if (Strings.Len(nodeCss) > 0)
            {
                link.CssClass = nodeCss;
            }
            link.RenderControl(writer);
        }

        protected void RenderOpenTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.m_objNode.ID);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.m_objNode.ID);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
        }
    }
}

